<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="薪级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="salaryGrade">
              <j-dict-select-tag :disabled=formDisabled type="list" v-model="model.salaryGrade" dictCode="salary_grade" placeholder="请选择薪级" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="基本工资（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="basicSalary">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0"  v-model="model.basicSalary" placeholder="请输入基本工资（元）" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="年功工资（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="yearSalary">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0"  v-model="model.yearSalary" placeholder="请输入年功工资" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="等级工资（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hierarchySalary">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0"  v-model="model.hierarchySalary" placeholder="请输入等级工资" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="岗位工资（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="postSalary">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" :precision="2"  v-model="model.postSalary" placeholder="请输入岗位工资" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="排序号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sort">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0" :precision="0"  v-model="model.sort" placeholder="请输入排序号" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea :disabled=formDisabled v-model="model.remark" rows="4" placeholder="请输入备注"  :max-length="500"/>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'ErpPayGradeForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
        salaryGrade: [
            { required: true, message: '请选择薪级!'},
            { validator: (rule, value, callback) => validateDuplicateValue('erp_pay_grade', 'salary_grade', value, this.model.id, callback)},
        ],
        basicSalary: [
            { required: true, message: '请输入基本工资!'},
        ],
        yearSalary: [
            { required: true, message: '请输入年功工资!'},
        ],
        hierarchySalary: [
            { required: true, message: '请输入等级工资!'},
        ],
        postSalary: [
            { required: true, message: '请输入岗位工资!'},
        ],
        sort: [
            { required: true, message: '请输入排序号!'},
        ],
        },
        url: {
          add: "/api/erp/hr/erpPayGrade/add",
          edit: "/api/erp/hr/erpPayGrade/edit",
          queryById: "/api/erp/hr/erpPayGrade/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>
